package cn.com.dwr.service.impl;

import cn.com.dwr.entity.AppProduct;
import cn.com.dwr.entity.AppSkuStock;
import cn.com.dwr.service.AppProductService;
import cn.com.dwr.service.AppSkuStockService;
import cn.com.dwr.service.AppUserService;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dwr.entity.AppCartItem;
import cn.com.dwr.service.AppCartItemService;
import cn.com.dwr.mapper.AppCartItemMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 邓文润
* @description 针对表【app_cart_item】的数据库操作Service实现
* @createDate 2023-12-12 07:13:55
*/
@Service
public  class AppCartItemServiceImpl extends ServiceImpl<AppCartItemMapper, AppCartItem>
    implements AppCartItemService{

    @Resource
    AppUserService appUserService;

    @Resource
    AppCartItemMapper appCartItemMapper;

    @Resource
    AppSkuStockService appSkuStockService;

    @Resource
    AppProductService appProductService;

    @Override
    public List<AppCartItem> getCurrent() {

        long userId = appUserService.getCurrentUserId();

        return appCartItemMapper.getCartItemsWithProductInfo(new QueryWrapper<AppCartItem>()
                .eq("user_id", userId)
                .orderByDesc("created"));
    }

    @Override
    public long getCount() {

        long userId = appUserService.getCurrentUserId();
        return this.count(new QueryWrapper<AppCartItem>()
                .eq("user_id", userId));
    }

    @Override
    public Object getTotal(List<Long> ids) {
        long userId = appUserService.getCurrentUserId();

        // 获取选中的购物车
        List<AppCartItem> cartItems = appCartItemMapper.getCartItemsWithProductInfo(new QueryWrapper<AppCartItem>()
                .eq("user_id", userId)
                .in("c.id", ids)
                .orderByDesc("created"));

        // 计算总金额/价格/数量
        BigDecimal total = cartItems.stream()
                .map(item -> item.getPrice().multiply(new BigDecimal(item.getQuantity())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        return MapUtil.of("total", total);
    }

    @Override
    public void updateQuantity(Long cartItemId, Integer quantity) {
        long userId = appUserService.getCurrentUserId();
        AppCartItem cartItem = this.getById(cartItemId);

        // 判断库存
        AppSkuStock skuStock = appSkuStockService.getSku(cartItem.getSkuId(), cartItem.getProductId());
        Assert.isTrue(skuStock.getStock() >= quantity, "库存不足");

        // 更新数量
        this.update(new UpdateWrapper<AppCartItem>()
                .eq("id", cartItemId)
                .eq("user_id", userId)
                .set("quantity", quantity)
                .set("updated", LocalDateTime.now()));
    }

    @Override
    public void add(AppCartItem appCartItem) {

        long userId = appUserService.getCurrentUserId();

        // 判断商品是否为空，是否上架
        AppProduct product = appProductService.getById(appCartItem.getProductId());
        Assert.isTrue(product != null && product.getIsOnSale(), "该商品已下架");

        // 判断库存是否足够
        AppSkuStock skuStock = appSkuStockService.getSku(appCartItem.getSkuId(), appCartItem.getProductId());
        Assert.isTrue(skuStock.getStock() >= appCartItem.getQuantity(), "库存不足");

        // 获取已存在的购物车商品
        AppCartItem existCartItem = this.getOne(new QueryWrapper<AppCartItem>()
                .eq("user_id", userId)
                .eq("product_id", appCartItem.getProductId())
                .eq("sku_id", appCartItem.getSkuId()));

        // 购物车已存在该商品
        if(existCartItem != null){
            // 原来数量加上新购买数量
            existCartItem.setQuantity(existCartItem.getQuantity() + appCartItem.getQuantity());
            existCartItem.setUpdated(LocalDateTime.now());
            this.updateById(existCartItem);
        }else{
            // 购物车未存在该商品
            appCartItem.setCreated(LocalDateTime.now());
            appCartItem.setUserId(userId);
            this.save(appCartItem);
        }
    }

    @Override
    public AppCartItem getCombinedCartItem(long userId, Long productId, Long skuId, Integer quantity) {

        AppProduct product = appProductService.getById(productId);
        Assert.isTrue(product != null && product.getIsOnSale(), "商品已下架");

        AppCartItem item = new AppCartItem();
        item.setProductId(productId);
        item.setSkuId(skuId);
        item.setQuantity(quantity);

        item.setProductName(product.getName());
        item.setProductImage(product.getImage());
        item.setProductSn(product.getSn());
        item.setCategoryId(product.getCategoryId());
        item.setUserId(userId);

        if(skuId == -1){
            item.setPrice(product.getPrice());
            item.setSku("默认规格");
        }else {
            AppSkuStock skuStock = appSkuStockService.getById(skuId);
            item.setSku(skuStock.getSku());
            item.setPrice(skuStock.getPrice());
        }
        return item;
    }
}




